package com.oauth.resource.controller;

import com.oauth.resource.entity.User;
import com.oauth.resource.vo.UserGetDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    private final static List<User> USER_LIST = new ArrayList<>();

    static {
        USER_LIST.add(new User(1, "小明", "男", 12));
        USER_LIST.add(new User(2, "小花", "女", 10));
        USER_LIST.add(new User(3, "小张", "男", 12));
    }

    @PostMapping(value = "/getDetail")
    public User getDetail(@RequestBody String body) {

        return null;
        //return USER_LIST.stream().filter(u->u.getId().equals(vo.getId())).findFirst().get();
    }

    /****************************请求地址上拼接查询参数******************************/

    @RequestMapping(value = "/queryParam", method = {RequestMethod.GET, RequestMethod.POST})
    public User queryParam(String name, Integer id) {
        log.info("入参：id={},name={}", id, name);
        return null;
    }

    @RequestMapping(value = "/queryParam2", method = {RequestMethod.GET, RequestMethod.POST})
    public User queryParam2(UserGetDetailVO vo, Integer age) {
        log.info("入参：vo={},age={}", vo, age);
        return null;
    }

    /****************************请求体form-data******************************/

    //正常访问
    @PostMapping(value = "/formData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public User formData(@RequestParam(required = false) Integer id, String name) {
        log.info("入参：id={},name={}", id, name);
        return null;
    }

    //正常访问，请求体正常
    @GetMapping(value = "/formData2", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public User formData4(@RequestParam(required = false) Integer id, String name) {
        log.info("入参：id={},name={}", id, name);
        return null;
    }


    /****************************请求体x-www-form-urlencoded******************************/

    // MediaType.APPLICATION_FORM_URLENCODED_VALUE方式，只有入参单一属性才能解密操作，
    // 其余从前端操作上不可行
    // post正常访问，get请求获取不到参数
    @PostMapping(value = "/xWwwFormUrlencoded", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public User xWwwFormUrlencoded(Integer id,String name) {
        log.info("入参：id={},name={}", id, name);
        return null;
    }

    //正常接收，格式：id=1&name=xiaoming
    @PostMapping(value = "/xWwwFormUrlencoded2", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public User xWwwFormUrlencoded2(@RequestBody String content) {
        log.info("入参：{}", content);
        return null;
    }

    //正常访问，格式：{id=[1], name=[xiaoming]}
    @PostMapping(value = "/xWwwFormUrlencoded3", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public User xWwwFormUrlencoded3(@RequestBody MultiValueMap<String, Object> map) {
        log.info("入参：{}", map);
        return null;
    }

    //正常访问，接收结果为 a=x&b=y
    @GetMapping(value = "/xWwwFormUrlencoded4", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public User xWwwFormUrlencoded4(@RequestBody String content) {
        log.info("入参：{}", content);
        return null;
    }

    //正常访问，MultiValueMap接收结果为 {id=[1]}
    @GetMapping(value = "/xWwwFormUrlencoded6", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public User xWwwFormUrlencoded6(@RequestBody MultiValueMap<String, Object> map) {
        log.info("入参：map={}", map);
        return null;
    }


    /****************************请求体raw(json,text,xml)******************************/

    @PostMapping(value = "/json", consumes = MediaType.APPLICATION_JSON_VALUE)
    public User json(@RequestBody UserGetDetailVO vo) {
        log.info("入参：{}", vo);
        return null;
    }

    @PostMapping(value = "/json2", consumes = MediaType.APPLICATION_JSON_VALUE)
    public User json2(@RequestBody UserGetDetailVO vo) {
        log.info("入参：{}", vo);
        return null;
    }

    @PostMapping(value = "/raw/text", consumes = MediaType.TEXT_PLAIN_VALUE)
    public User textPlain(@RequestBody String content) {
        log.info("入参：{}", content);
        return null;
    }

    @PostMapping(value = "/raw/xml", consumes = MediaType.APPLICATION_XML_VALUE)
    public User textXml(@RequestBody String content) {
        log.info("入参：{}", content);
        return null;
    }

}
